using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineTest.UC;
using TestsBase.DB;
using TestsBase.lib;

namespace OnlineTest
{
    public partial class _Default : Page
    {
        private string indexPage = "Default.aspx";
        private int pos = 0;
        private TestDatabase Quiz;

        protected void Page_Load(object sender, EventArgs e)
        {
            PlaceHolder1.Controls.Clear();
            Quiz = (TestDatabase) Session["Quiz"];

            if (Request["quizName"] != null && Quiz == null)
            {
                try
                {
                    XMLLoader xml = new XMLLoader(MapPath("db/" + Request["quizName"] + ".xml"));
                    TestDatabase quizLoaded = xml.Fetch();
                    Quiz = quizLoaded;
                    Quiz.First();
                    InitControls();
                    Session["Quiz"] = quizLoaded;
                    Response.Redirect(indexPage, true);
                    return;
                }
                catch
                {
                    Label errorLabel = new Label();
                    errorLabel.Text = "Sorry. Error loading quiz.";
                    PlaceHolder1.Controls.Add(errorLabel);
                    return;
                }
            }

            if (Quiz == null)
            {
                string[] dbFiles = Directory.GetFiles(MapPath("db"));
                foreach (string file in dbFiles)
                {
                    HyperLink dbFileLabel = new HyperLink();
                    dbFileLabel.Text = Path.GetFileNameWithoutExtension(file);
                    dbFileLabel.NavigateUrl = indexPage + "?quizName=" +
                                              Path.GetFileNameWithoutExtension(file);
                    PlaceHolder1.Controls.Add(dbFileLabel);
                }
                return;
            }
            InitControls();
            

            /*
            
            //Regex test
			TestDatabase quiz = new TestDatabase(10);
			//quiz.Tests.add(new Test())
			
            Test myTest = new Test(TestType.Regex, "RegexTest");
            myTest.Question = "qust";
            Regex rx = new Regex(@"^t(e+)st$");
            myTest.LoadAnswers(null, rx);
            myTest.ParseUserData("teeeeeeeeeeest");
            Label lb = new Label();
            lb.Text = string.Format("['{0}']['{1}']['{2}']<br>\n", myTest.Name, myTest.Question, myTest.Result.ToString());
            PlaceHolder1.Controls.Add(lb);

            //CheckBox test
            Test myTest01 = new Test(TestType.CheckBox, "CheckTest");
            myTest01.Question = "???";
            List<string> answers = new List<string>();
	            answers.Add("1");
	            answers.Add("2");
	            answers.Add("3");
	            answers.Add("4");
            List<string> correct = new List<string>();
	            correct.Add("3");
	            correct.Add("2");
            myTest01.LoadAnswers(answers, correct);
            List<string> userData = new List<string>();
            userData.Add("2");
            myTest01.ParseUserData(userData);
            Label lb01 = new Label();
            lb01.Text = string.Format("['{0}']['{1}']['{2}']<br>\n", myTest01.Name, myTest01.Question, myTest01.Result.ToString());
            PlaceHolder1.Controls.Add(lb01);

            //TestDatabase quiz = 


            //myTest.LoadAnswers()
            
            if (null != Session["Quiz"])
            {
                Quiz = (TestDatabase) Session["Quiz"];
                RefreshVisual();
            }
             
             
            */
        }

        protected void prevBtn_Click(object sender, EventArgs e)
        {
            if (null != Session["Quiz"])
            {
                Quiz = (TestDatabase) Session["Quiz"];
                if (!Quiz.Prev()) return;
                InitControls();
            }
        }

        protected void nextBtn_Click(object sender, EventArgs e)
        {
            if (null != Session["Quiz"])
            {
                Quiz = (TestDatabase) Session["Quiz"];
                if (!Quiz.Next()) return;
                InitControls();
            }
        }

        protected void InitControls()
        {
            if (null != Session["Quiz"])
            {
                PlaceHolder1.Controls.Clear();

                //prevBtn.Visible = true;
                //nextBtn.Visible = true;
                btnSubmit.Visible = true;

                cblTestList.Items.Clear();
                cblTestList.Enabled = false;
                for (int i = 0; i < Quiz.Tests.Count; i++)
                {
                    cblTestList.Items.Add(Quiz.Tests[i].Name);
                }
                cblTestList.SelectedIndex = Quiz.Position;


                
                Label testName = new Label();
                testName.ID = "testName";
                testName.Text = Quiz.SelectedTest.Name;
                PlaceHolder1.Controls.Add(testName);

                Label testQuestion = new Label();
                testQuestion.ID = "testQuestion";
                testQuestion.Text = Quiz.SelectedTest.Question;
                PlaceHolder1.Controls.Add(testQuestion);
                
                Control ucCtrl = new Control();
                switch (Quiz.SelectedTest.Type)
                {
                    case TestType.Regex:
                        ucCtrl = Page.LoadControl("UC\\UCTestRegex.ascx");
                        break;
                    case TestType.CheckBox:
                        ucCtrl = Page.LoadControl("UC\\UCTestCheckBox.ascx");
                        ((UCTestCheckbox)ucCtrl).LoadData(Quiz.SelectedTest.LoadedData);
                        break;
                    case TestType.RadioBtn:
                        ucCtrl = Page.LoadControl("UC\\UCTestRadio.ascx");
                        ((UITestRadio) ucCtrl).LoadData(Quiz.SelectedTest.LoadedData);
                        break;
                    default:
                        break;
                }

                ucCtrl.ID = "testAnswers";
                Session["UC"] = ucCtrl;
                PlaceHolder1.Controls.Add(ucCtrl);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if(null != Session["UC"])
            {
                object data = ((UCTest) Session["UC"]).GetData();
                Quiz.SelectedTest.ParseUserData(data);

                if (Quiz.Position == Quiz.Tests.Count - 1)
                {
                    Response.Redirect("Result.aspx");
                    return;
                }
                nextBtn_Click(null,null);
            }
        }

    }
}